package com.dc.flow

import androidx.lifecycle.viewModelScope
import com.dc.flow.ext.requestByFlow
import com.dc.flow.network.BaseResult
import com.dc.flow.vm.BaseFlowViewModel
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch

class FlowViewModel : BaseFlowViewModel() {

    private val _requestInfo = MutableStateFlow<BaseResult<String>>(BaseResult.Loading(false))
    val requestInfo = _requestInfo.asStateFlow()

    fun requestInfo() {
        viewModelScope.launch {
            requestByFlow {
                coroutineScope {
                    // 摸拟网络请求耗时操作
                    delay(500)
                    "网络数据"
                }
            }.collect {
                _requestInfo.value = it
            }
        }
    }
}